package andy.ham;

import android.app.ListActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

import andy.ham.DaoMaster.DevOpenHelper;

//继承来自listView
public class MyDiary extends ListActivity{

	public static final int MENU_ITEM_INSERT = Menu.FIRST;
	public static final int MENU_ITEM_RETURN = Menu.FIRST+1;
	public static final int MENU_ITEM_DELETE = Menu.FIRST+2;


	// GreenDAO使用的变量
	private SQLiteDatabase db;
	private DaoMaster daoMaster;
	private DaoSession daoSession;
	private NoteDao noteDao;
	private Cursor cursor;




	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.diary_list);
		setTitle("MyDiary");

		//长按删除
		ListView lv = getListView();
		lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View v, int position, final long id) {
				final AlertDialog.Builder builder=new AlertDialog.Builder(MyDiary.this,R.style.AlertDialogCustom);
				builder.setTitle("删除日记");
				builder.setMessage("您确定要删除此条日记吗?");
				builder.setPositiveButton("确定",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {
						noteDao.deleteByKey(id);
						Toast.makeText(MyDiary.this,"删除成功", Toast.LENGTH_LONG).show();
						cursor.requery();
					}
				});
				builder.setNegativeButton("取消",new DialogInterface.OnClickListener(){
					@Override
					public void onClick(DialogInterface dialog, int which) {

					}
				});
				builder.create();
				builder.show();
				return true;
			}
		});



		// 初始化GreenDAO
		InitDAO();

		// 初始化List
		InitList();

	}

	private void InitDAO() {
		DevOpenHelper helper = new DaoMaster.DevOpenHelper(this, "notes-db",
				null);
		db = helper.getWritableDatabase();
		daoMaster = new DaoMaster(db);
		daoSession = daoMaster.newSession();
		noteDao = daoSession.getNoteDao();
	}


	private void InitList() {
		String textColumn = NoteDao.Properties.Title.columnName;
		String dateColunm = NoteDao.Properties.Date.columnName;
		String orderBy = dateColunm + " COLLATE LOCALIZED DESC";
		cursor = db.query(noteDao.getTablename(), noteDao.getAllColumns(),
				null, null, null, null, orderBy);
		String[] from = { textColumn, dateColunm };
		int[] to = { R.id.text1, R.id.created };

		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.diary_row, cursor, from, to);
		setListAdapter(adapter);
	}


	// 添加选择菜单
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		//add方法的四个参数分别为group id、item id、order id及title
		menu.add(Menu.NONE, MENU_ITEM_INSERT, 0, "写日记");
		menu.add(Menu.NONE, MENU_ITEM_DELETE, 0, "清空日记");
		menu.add(Menu.NONE, MENU_ITEM_RETURN, 0, "退出登录");
		return true;
	}

	// 添加选择菜单的选择事件
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
			case MENU_ITEM_INSERT:
				Intent intent0 = new Intent(this, DiaryEditor.class);
				intent0.setAction(DiaryEditor.INSERT_DIARY_ACTION);
				intent0.setData(getIntent().getData());
				startActivity(intent0);
				MyDiary.this.finish();
				cursor.requery();
				return true;


			case MENU_ITEM_DELETE:
				noteDao.deleteAll();
				Toast.makeText(MyDiary.this,"清空成功",
						Toast.LENGTH_LONG).show();
				cursor.requery();
				return true;

			case MENU_ITEM_RETURN:
				Intent intent1 = new Intent(this, LoginActivity.class);
				startActivity(intent1);
				MyDiary.this.finish();
				cursor.requery();
				return true;
		}
		return super.onOptionsItemSelected(item);
	}

	//列表内日记点击事件
	protected void onListItemClick(ListView l, View v, int position, long id) {
		String mid = Long.toString(id);
		Log.d("id", mid);

		Intent intent = new Intent(this, DiaryEditor.class);
		intent.setAction(DiaryEditor.EDIT_DIARY_ACTION);
		Bundle bundle = new Bundle();
		bundle.putLong("id", id);
		intent.putExtras(bundle);
		Log.d("id", mid);
		startActivity(intent);
		MyDiary.this.finish();
		cursor.requery();
	}



	protected void onActivityResult(int requestCode, int resultCode,
			Intent intent) {
		cursor.requery();
		super.onActivityResult(requestCode, resultCode, intent);
	}
}
